package com.huawang.mdesign.model.service.util.uaf;

import com.huawang.core.definition.mof.instances.MetaClassInstance;
import org.apache.commons.collections4.CollectionUtils;

import java.util.List;

public class SortUtils {

    private SortUtils() {}

    /**
     * 根据OrderRegulation列表的顺序，对目标列表进行排序
     */
    public static void setListOrder(List<String> orderRegulation, List<MetaClassInstance> targetList) {
        if (CollectionUtils.isNotEmpty(targetList)) {
            // 按照 list 里的 name 来排序 targetList
            targetList.sort(((o1, o2) -> {
                int io1 = orderRegulation.indexOf(o1.getUUID());
                int io2 = orderRegulation.indexOf(o2.getUUID());
                if (io1 != -1) {
                    io1 = targetList.size() - io1;
                }
                if (io2 != -1) {
                    io2 = targetList.size() - io2;
                }
                return io2 - io1;
            }));
        }
    }
}
